#include"List.h"

LTNode* BuyListNode(LTDataType x)
{
    LTNode* node = (LTNode*)malloc(sizeof(LTNode));
    if(node == NULL)
    {
        perror("malloc fail\n");
        exit(-1);
    }
    node->data = x;
    node->next = NULL;
    node->prev = NULL;

    return node;
}

//void ListInit(LTNode* phead)
//{
//    phead = BuyListNode(-1);
//    phead->next = phead;
//    phead->prev = phead;
//}
//
//
//
//void ListInit(LTNode** pphead)
//{
//    *pphead = BuyListNode(-1);
//    (*pphead)->next = *pphead;
//    (*pphead)->prev = *pphead;
//}
LTNode* ListInit()
{
    LTNode* phead = BuyListNode(-1);
    phead->next = phead;
    phead->prev = phead;
    return  phead;
}
void ListPrint(LTNode* phead)
{
    assert(phead);
    LTNode* cur = phead->next;
    while(cur != phead)
    {
        printf("%d ",cur->data);
        cur = cur->next;
    }
    printf("\n");
}
void ListPushBack(LTNode* phead, LTDataType x)
{
    assert(phead);
 //   LTNode* newnode = BuyListNode(x);
 //   LTNode* tail = phead->prev;

 //   tail->next = newnode;
 //   newnode->next = phead;
 //   newnode->prev = tail;
 //   phead->prev = newnode;
    ListInsert(phead, x);
}
//void ListPushFront(LTNode* phead, LTDataType x)
//{
//    assert(phead);
//    LTNode* newnode = BuyListNode(x);
//
//    LTNode* next = phead->next;
//
//    newnode->next = next;
//    next->prev = newnode;
//    newnode->prev = phead;
//    phead->next = newnode;
//
//}
void ListPushFront(LTNode* phead, LTDataType x)
{
    assert(phead);
    //LTNode* newnode = BuyListNode(x);

    //newnode->next = phead->next;
    //phead->next->prev = newnode;

    //phead->next = newnode;
    //newnode->prev = phead;
    ListInsert(phead->next, x);
}
bool ListEmpty(LTNode* phead)
{
    assert(phead);

    return phead->next == phead;
}
void ListPopBack(LTNode* phead)
{
    assert(phead);
   // assert(phead->next != phead);
    assert(!ListEmpty(phead));

   // LTNode* tail = phead->prev;
   // LTNode* tailprev = tail->prev;

   // tailprev->next = phead;
   // phead->prev = tailprev;

   // free(tail);
   ListErase(phead->prev);

}
void ListPopFront(LTNode* phead)
{
    assert(phead);
    assert(!ListEmpty(phead));

   // LTNode* head = phead->next;

   // phead->next = head->next;
   // head->next->prev = phead;

   // free(head);
   ListErase(phead->next);

}
void ListInsert(LTNode* pos, LTDataType x)
{
    assert(pos);

    LTNode* newnode = BuyListNode(x);

    LTNode* prev = pos->prev;

    prev->next = newnode;
    newnode->next = pos;
    pos->prev = newnode;
    newnode->prev = prev;
}
void ListErase(LTNode* pos)
{
    assert(pos);
    
    pos->prev->next = pos->next;
    pos->next->prev = pos->prev;

    free(pos);
}
int ListSize(LTNode* phead)
{
    LTNode* cur = phead->next;
    int size = 0;
    while(cur != phead)
    {
        size++;
        cur = cur->next;
    }
    return size;
}
void ListDestory(LTNode* phead)
{
    
    LTNode* cur = phead->next;
    LTNode* next = NULL;
    while(cur != phead)
    {
        next = cur->next;
        ListErase(cur);
        cur = next;
    }
    free(phead);
    phead = NULL;
}
